package com.rayboo.web.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rayboo.model.ClubCar;
import com.rayboo.service.ClubCarService;
import com.rayboo.util.GridPaging;
import com.rayboo.util.JsonTool;
import com.rayboo.vo.Result;

/**
 * 
 * ClubCarController
 * 
 * @author ray
 * @date 2016年9月8日 下午8:03:09
 *
 */
@Controller
@RequestMapping("/cars")
public class ClubCarController {

	@Autowired
	ClubCarService clubCarTypeService;

	/**
	 * 汽车列表页面
	 * 
	 * @return
	 */
	@RequestMapping(method = { RequestMethod.GET })
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response, GridPaging gridPaging,
			@RequestParam(required = false, defaultValue = "", value = "query") String query) {
		ModelAndView mav = new ModelAndView("cars/index");
		Integer total = clubCarTypeService.count(query);
		List<ClubCar> lst = clubCarTypeService.find(query, gridPaging.genPageable(total));
		mav.addObject("lst", lst);
		mav.addObject("query", query);
		mav.addObject("gridPaging", gridPaging);
		return mav;
	}

	/**
	 * 汽车编辑页面
	 * 
	 * @param request
	 * @param response
	 * @param carid
	 * @return
	 */
	@RequestMapping(value = "/edit/{carid}", method = { RequestMethod.GET })
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response,
			@PathVariable(value = "carid") Integer carid) {
		ModelAndView mav = new ModelAndView("cars/edit");
		ClubCar car = clubCarTypeService.findByCarId(carid);
		mav.addObject("car", car);
		return mav;
	}
	
	/**
	 * 添加汽车信息
	 * 
	 * @param request
	 * @param response
	 * @param carType
	 * @return
	 */
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public Result create(HttpServletRequest request, HttpServletResponse response,
			ClubCar carType) {
		if (carType == null)
			return Result.genFail("carType empty");
		return Result.genSuccess("添加成功");
	}

	/**
	 * 更新汽车信息
	 * 
	 * @param request
	 * @param response
	 * @param userid
	 * @param jsonString
	 * @return
	 */
	@RequestMapping(value = "/update/{carid}", method = RequestMethod.POST)
	@ResponseBody
	public Result update(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer carid,
			ClubCar car) {
		if (car == null)
			return Result.genFail("car empty");
		clubCarTypeService.update(carid, car);
		return Result.genSuccess("更新成功");
	}

	/**
	 * 更新汽车简称
	 * 
	 * @param request
	 * @param response
	 * @param carid
	 * @param carType
	 * @return
	 */
	@RequestMapping(value = "/update/{carid}/sname", method = RequestMethod.POST)
	@ResponseBody
	public Result updateSname(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer carid,
			@RequestBody String jsonString) {
		if (carid == 0 || jsonString == null)
			return Result.genFail("parameter error");
		ClubCar car = JsonTool.getObject(jsonString, ClubCar.class);
		if (car == null)
			return Result.genFail("car empty");
		clubCarTypeService.updateSname(carid, car.getSname());
		return Result.genSuccess("更新成功");
	}

}
